import { execute } from "../../database/connector";

import { IdentityQueries } from "../queries/identity.query";
import { IIdentity } from "../../models/identity.model";

/**
 * gets active identitys
 */
export const getIdentitys = async () => {
  return execute<IIdentity[]>(IdentityQueries.GetIdentitys, []);
};

/**
 * gets a identity based on id provided
 */
export const getIdentityById = async (id: IIdentity['id']) => {
  return execute<IIdentity>(IdentityQueries.GetIdentityById, [id]);
};

/**
 * adds a new active identity record
 */
export const insertIdentity = async (identity: IIdentity) => {
  const result = await execute<{ affectedRows: number }>(IdentityQueries.AddIdentity, [
    Object.values(identity)
  ]);
  return result.affectedRows > 0;
};

/**
 * updates identity information based on the id provided
 */
export const updateIdentity = async (identity: IIdentity, fields: string) => {
  const result = await execute<{ affectedRows: number }>(IdentityQueries.UpdateIdentityById, [fields, identity.id]);
  return result.affectedRows > 0;
};

/**
 * updates identity information based on the id provided
 */
 export const deleteIdentity = async (id: IIdentity['id']) => {
  const result = await execute<{ affectedRows: number }>(IdentityQueries.DeleteIdentityById, [
    id
  ]);
  return result.affectedRows > 0;
};